package com.liuzu.esys.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.liuzu.esys.bo.ItemBo;
import com.liuzu.esys.common.LayuiPageVo;
import com.liuzu.esys.common.R;
import com.liuzu.esys.listener.WebItemListener;
import com.liuzu.esys.pojo.Item;

import com.liuzu.esys.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/item")
public class ItemController {
    @Resource
    private ItemService itemService;
    //分页和模糊查询的接口
    @RequestMapping("/itemList")
    public LayuiPageVo<Item> itemPageData(@RequestParam Map<String,Object> params){
       // System.out.println(params);
        return itemService.itemPageData(params);
    }
    //添加合作项目
    @GetMapping("/addItem")
    public R addItem(Item item) {

        System.out.println(item);
        int result = itemService.addItem(item);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }

    }
    //删除合作项目
    @RequestMapping("/deleteItem")
    public R deleteItem(Integer id){
        int i = itemService.deleteItem(id);
        if (i == 1) {
            return R.ok();
        }else {
            return R.error();
        }
    }
    //修改合作项目
    @RequestMapping("/updateItem")
    public R updateItem(Item item){
        System.out.println(item);
        int i = itemService.updateItem(item);
        if (i == 1) {
            return R.ok();
        }else{
            return R.error();
        }
    }
    @Autowired
    private WebItemListener webItemListener;

    //导出列表数据     把满足条件的数据 查询出来  写到excel 中  ，把excel 写到浏览器
    @RequestMapping("/export")
    public void export(Item item, HttpServletResponse response) throws IOException {
        //下载excel 的 响应数据格式
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 下载的文件名 是中文  要进行转码  要不然会出现乱码
        String filename = URLEncoder.encode("项目合作信息导出", "utf-8");

        // 告诉浏览器  下载文件
        response.setHeader("Content-Disposition", "attachment;filename=" + filename + ".xlsx");

        //得到指向浏览器的输出流  向浏览器写输出
        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream, ItemBo.class);

        ExcelWriterSheetBuilder sheet = write.sheet();
        //准备满足条件的集合数据
        List<ItemBo> students = itemService.itemExcelData(item);
        // 把这个集合数据 写到 输出流
        sheet.doWrite(students);
    }

}
